package com.coollang.smartbra.fragment;

import android.app.Dialog;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.VideoView;

import com.coollang.smartbra.R;
import com.coollang.smartbra.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;


/**
 * DialogFragment
 */

public class MyDialogFragment extends DialogFragment {

    private static final String TAG = "MyDialogFragment";
    @BindView(R.id.video_view)
    VideoView mVideoView;
    @BindView(R.id.pb_progressbar)
    ProgressBar mProgressBar;
    @BindView(R.id.action_title)
    TextView mActionTitle;

    private Dialog mDialog;
    Unbinder unbinder;

    private Handler mHandler = new Handler();

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        View customView = getActivity().getLayoutInflater().inflate(R.layout.custom_dialog, null);
        unbinder = ButterKnife.bind(this, customView);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setView(customView);

        mDialog = builder.create();
        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        mDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);

        return mDialog;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.0f;
        window.setAttributes(windowParams);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 播放视频
        playVideo();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        mVideoView.stopPlayback();
        mHandler.removeCallbacksAndMessages(null);
        unbinder.unbind();
    }

    @OnClick({R.id.tv_last, R.id.tv_next})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_last:

                if (mlistener.onClickLast() == 0) {
                    ToastUtils.showToast("已经是第一条了");
                } else {
                    playVideo();
                }

                break;
            case R.id.tv_next:
                // 获取下一个视频的
                if (mlistener.onClickNext() == 0) {
                    ToastUtils.showToast("已经是最后一条了");
                } else {
                    playVideo();
                }
                break;
        }
    }

    public void playVideo() {
        // 找activity要path路径
        mVideoView.setVideoPath(mlistener.getVideoPath());
        start();
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                start();
            }
        });
    }

    private void start() {
        mVideoView.start();
        mProgressBar.setMax(mVideoView.getDuration() / 10);//开始进度条
        setTime();
    }

    private void setTime() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                performUpDateUi();
                mHandler.postDelayed(this, 10);
            }
        });
    }

    private void performUpDateUi() {
        mProgressBar.setMax(mVideoView.getDuration() / 10);
        mProgressBar.setProgress(mVideoView.getCurrentPosition() / 10);
    }

    // ==================== interface ==================
    public OnDialogListener mlistener;

    public interface OnDialogListener {
        // 获取第一次播放的视频
        String getVideoPath();

        int onClickNext();

        int onClickLast();
    }

    public void setOnDialogListener(OnDialogListener dialogListener) {
        this.mlistener = dialogListener;
    }
}